package com.heifeng.cloudshelf.widget;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.SeekBar;


import com.heifeng.cloudshelf.R;

import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

/**
 * *这里可以监听到视频播放的生命周期和播放状态
 * 所有关于视频的逻辑都应该写在这里
 * Created by admin on 2019/8/19.
 */

public class CustomVideoPlayer extends JzvdStd {
    public interface ShareClick {
        void onShareClick();
    }

    public interface PayClick {//成为会员、付费点击事件

        void onClick();
    }

    public PayClick payClick;

    public ShareClick shareClick;

    public void setShareClick(ShareClick shareClick) {
        this.shareClick = shareClick;
    }

    public void setPayClick(PayClick payClick) {
        this.payClick = payClick;
    }

    public boolean allowPlay = false;//是否允许播放

    public boolean isAllowPlay() {
        return allowPlay;
    }

    public void setAllowPlay(boolean allowPlay) {
        this.allowPlay = allowPlay;
        setAllowPlayUi(allowPlay);
    }

    public CustomVideoPlayer(Context context) {
        super(context);
    }

    public CustomVideoPlayer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void init(Context context) {
        super.init(context);
        setAllowPlayUi(allowPlay);
    }

    public void setAllowPlayUi(boolean AllowPlay) {
//        startButton.setVisibility(AllowPlay ? VISIBLE : GONE);
//        lBuy.setVisibility(AllowPlay ? GONE : VISIBLE);
//        lBuy.setVisibility(GONE);
        setAllow(allowPlay);
    }

    public void setLayoutTopBgNull() {
        layoutTop.setBackground(null);
        fullscreenButton.setVisibility(VISIBLE);//显示全屏按钮
    }

    public void setPayTips(String text, String btnText) {//设置视频需要付费的相关信息，（开通成为会员即可观看，成为会员）
        tvTips.setText(text);
        tvBtnBuy.setText(btnText);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();

        if (i == R.id.iv_back) {//返回
            ((Activity) super.getContext()).onBackPressed();
            return;
        }
        if (i == R.id.iv_share) {//分享
            if (shareClick != null)
                shareClick.onShareClick();
            return;
        }
        if (i == R.id.tv_btn) {//付费
            if (payClick != null)
                payClick.onClick();
            return;
        }
        if (allowPlay) {
            super.onClick(v);
            if (i == cn.jzvd.R.id.fullscreen) {
                Log.i(TAG, "onClick: fullscreen button");
            } else if (i == R.id.start) {
                Log.i(TAG, "onClick: start button");
            }
        } else
            return;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (allowPlay) {
            super.onTouch(v, event);
            int id = v.getId();
            if (id == cn.jzvd.R.id.surface_container) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_UP:
                        if (mChangePosition) {
                            Log.i(TAG, "Touch screen seek position");
                        }
                        if (mChangeVolume) {
                            Log.i(TAG, "Touch screen change volume");
                        }
                        break;
                }
            }
        } else {

        }

        return false;
    }

    @Override
    public int getLayoutId() {
        return R.layout.jz_layout_std;
    }

    @Override
    public void startVideo() {
        super.startVideo();
        Log.i(TAG, "startVideo");
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        super.onStopTrackingTouch(seekBar);
        Log.i(TAG, "Seek position ");
    }

    @Override
    public void gotoScreenFullscreen() {
        super.gotoScreenFullscreen();
        Log.i(TAG, "goto Fullscreen");
        rBackShare.setVisibility(GONE);
    }

    @Override
    public void gotoScreenNormal() {
        super.gotoScreenNormal();
        Log.i(TAG, "quit Fullscreen");
        rBackShare.setVisibility(GONE);
    }

    @Override
    public void autoFullscreen(float x) {
        super.autoFullscreen(x);
        Log.i(TAG, "auto Fullscreen");
    }

    @Override
    public void onClickUiToggle() {
        super.onClickUiToggle();
        Log.i(TAG, "click blank");
    }

    //onState 代表了播放器引擎的回调，播放视频各个过程的状态的回调
    @Override
    public void onStateNormal() {
        super.onStateNormal();
    }

    @Override
    public void onStatePreparing() {
        super.onStatePreparing();
    }

    @Override
    public void onStatePlaying() {
        super.onStatePlaying();
    }

    @Override
    public void onStatePause() {
        super.onStatePause();
    }

    @Override
    public void onStateError() {
        super.onStateError();
    }

    @Override
    public void onStateAutoComplete() {
        super.onStateAutoComplete();
        Log.i(TAG, "Auto complete");
    }

    //changeUiTo 真能能修改ui的方法
    @Override
    public void changeUiToNormal() {
        super.changeUiToNormal();
    }

    @Override
    public void changeUiToPreparing() {
        super.changeUiToPreparing();
    }

    @Override
    public void changeUiToPlayingShow() {
        super.changeUiToPlayingShow();
    }

    @Override
    public void changeUiToPlayingClear() {
        super.changeUiToPlayingClear();
    }

    @Override
    public void changeUiToPauseShow() {
        super.changeUiToPauseShow();
    }

    @Override
    public void changeUiToPauseClear() {
        super.changeUiToPauseClear();
    }

    @Override
    public void changeUiToComplete() {
        super.changeUiToComplete();
    }

    @Override
    public void changeUiToError() {
        super.changeUiToError();
    }

    @Override
    public void onInfo(int what, int extra) {
        super.onInfo(what, extra);
    }

    @Override
    public void onError(int what, int extra) {
        super.onError(what, extra);
    }
}
